<?php
/**
 * 岗位管理
 *
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class RightController extends BaseController {

	public function index()
	{	$psize = I('get.psize', $this->m_psize,'intval');
		if( !$psize )
			$psize = $this->m_psize;
		else if( $psize > 20 )
			$psize = $this->m_psize;

		$cnt = M("right")->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );
		$list = M("right")->order("right_id asc")->limit($Page->firstRow.','.$Page->listRows )->select();
		$res = array('rcode'=>1,'data'=>$list,'msg'=>'ok' );
		e_json( $res );
	}

	public function add()
	{
		if( IS_POST ){
			$Mdo = D("Right");
			$name = I('post.right_name','','trim');
			$code = I('post.code','','trim');
			$info = I('post.info','','trim');
			$rights = isset( $_POST['rights']) ? (array)$_POST['rights'] : array();
			$res = array('rcode'=>0,'data'=>$list,'msg'=>'error' );
			$rightContent = "";
			$right_content = '';
			$right_object  = array();
			foreach( $rights as $vr){
				$right_object[$vr['code']] = $vr;
			}
			$right_content = serialize( $right_object );
			$dres = $Mdo->add_right( $name, $code, $info, $right_content );
			if( $dres['r']){
				$res['rcode'] =1;
				$res['data'] = $dres['d'];
			}
			else{
				$res['rcode'] = 0;
				$res['msg'] = $dres['i'];
			}
 			e_json( $res );
		}
	}

	/**
	 * 编辑权限信息
	 *
	 * @get     编辑对象的数据
	 * @post    提交修改
	 * @return array
	 */
	public function edit(){
		$res = array('rcode'=>0,'data'=>$list,'msg'=>'error' );
		$Mdo = D("Right");

		if( IS_GET ){
			$id =  I('get.id','false', 'trim');
			$wheres = array();
			if( $id ){
				$wheres['right_id'] = array('eq', $id );
				$data = $Mdo->where( $wheres )->find();
				if( !$data ){
					$res['msg'] = L('NO_DATA');
				}else{
					$res['rcode'] = 1;
					$res['msg'] = 'ok';
					$data['right_content'] = unserialize( $data['right_content'] );
					$res['data'] = $data;
				}
			}
			else
				$res['msg'] = L("PARAM_WRONG");

		}
		else{
			$id = I('post.right_id','false','intval');
			$name = I('post.right_name','','trim');
			$code = I('post.code','','trim');
			$info = I('post.info','','trim');
			$rights = isset( $_POST['rights']) ? (array)$_POST['rights'] : array();
			$res = array('rcode'=>0,'data'=>'','msg'=>'error' );
			$rightContent = "";
			$right_content = '';
			$right_object  = array();
			foreach( $rights as $vr){
				$right_object[$vr['code']] = $vr;
			}
			$right_content = serialize( $right_object );
			$dres = $Mdo->edit_right($id, $name, $code, $info, $right_content );
			if( $dres['r']){
				$res['rcode'] =1;
				$res['data'] = $dres['d'];
			}
			else{
				$res['rcode'] = 0;
				$res['msg'] = $dres['i'];
			}
		}

		e_json( $res );
	}

	/**
	 * 删除权限集合
	 *
	 * @return array
	 */
	public function delete()
	{

		if( IS_POST ){
			$Mdo = D("Right");
			$res = array('rcode'=>0,'data'=>'','msg'=>'error' );
			$id = I('post.id','false','intval');
			$dres = $Mdo->del_right( $id );
			if( $dres['r']){
				$res['rcode'] = 1;
				$res['msg'] = "OK";
			}
			else{
				$res['msg'] = $dres['i'];
			}

			e_json( $res );
		}
	}

	/**
	 * 获取xml定义的权限集合
	 *
	 * @return array mixed
	 */
	public function getconfig(){
		$res = array('rcode'=>1,'data'=>false,'msg'=>'ok' );
		$MRight = D("Right");
		$datacfg = $MRight->xml2arr();
		if( $datacfg ){
			$res['rcode'] = 1;
			$res['msg'] = "OK";
			$res['data'] = $datacfg;
		}
		else{
			$res['rcode'] = 0;
			$res['msg'] = "获取配置信息失败";
		}
		e_json( $res );
	}
}